package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.BpProductionFacilityVideoBean;
import com.joysuch.wwyt.bp.bean.BpSystemVideoAddBean;
import com.joysuch.wwyt.bp.service.BpProductionFacilityVideoService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xsx
 * @version 1.0
 * @date 2022/10/12 11:42
 */
@RestController
@RequestMapping("/bp/device-facility-video")
@Api(tags = "设备设施管理-设备检测")
public class BpProductionFacilityVideoController {
    private static final Logger logger = LoggerFactory.getLogger(BpProductionFacilityVideoController.class);

    @Autowired
    private BpProductionFacilityVideoService bpProductionFacilityVideoService;


    @PostMapping("save")
    @ApiOperation("新增/修改")
    public ResultBean save(@RequestBody @Valid BpProductionFacilityVideoBean bean) {
        return bpProductionFacilityVideoService.save(bean);
    }

    @GetMapping("page")
    @ApiOperation(value = "基于设备装置id分页查询", response = BpProductionFacilityVideoBean.class)
    public ResultBean page(Pageable pageable, Long deviceFacilityId) {
        Page<BpProductionFacilityVideoBean> data = bpProductionFacilityVideoService.page(pageable, deviceFacilityId);
        List<BpProductionFacilityVideoBean> list = data.getContent().stream().filter(r -> r.getId() != null).collect(Collectors.toList());
        return ResultBean.pageData(list, list.size());
    }

    @GetMapping("findVideoPage")
    @ApiOperation(value = "查询设备装置未绑定的视频设备")
    public ResultBean findVideoPage(Pageable pageable, Long deviceFacilityId){
        Page<BpSystemVideoAddBean> list = bpProductionFacilityVideoService.findVideoPage(pageable, deviceFacilityId);
        return ResultBean.pageData(list);
    }

    @RequestMapping("delete")
    public Object delete(Long id) {
        try {
            bpProductionFacilityVideoService.delete(id);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("删除失败", e);
            return ResultBean.fail(101, "删除失败");
        }
    }


    @GetMapping("get/{id}")
    @ApiOperation(value = "根据id查询单条数据", response = BpProductionFacilityVideoBean.class)
    public ResultBean getById(@PathVariable Long id) {
        BpProductionFacilityVideoBean bean = bpProductionFacilityVideoService.getById(id);
        return ResultBean.success(bean);
    }


}
